package edu.cth.speedapp.teacher;

import edu.cth.speedapp.DeviceUuidFactory;
import edu.cth.speedapp.HttpRequest;
import edu.cth.speedapp.R;
import edu.cth.speedapp.asynctasks.HTTPAsyncTask;
import edu.cth.speedapp.asynctasks.ITaskCallback;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class CreateRoomActivity extends Activity implements ITaskCallback {
	EditText roomNameTextField;
	View mainView;
	View createProgress;
	View createButton;
	
	private String roomName = "";
	private String deviceUuid = null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_room);
        mainView = getWindow().getCurrentFocus();
        
        // Android device ID
        DeviceUuidFactory deviceUuidFactory = new DeviceUuidFactory(this);
        deviceUuid = deviceUuidFactory.getDeviceUuid().toString();      
        
        createProgress = findViewById(R.id.createRoomProgress);
        roomNameTextField = (EditText) findViewById(R.id.createRoomNameText); 
        createButton = findViewById(R.id.createRoomButtom);
        
        
        
        setInputEnabled(true);
        
    }

    private void setInputEnabled(boolean enabled){
    	if(enabled){
    		createProgress.setVisibility(View.INVISIBLE);
    		
    	}else{
    		createProgress.setVisibility(View.VISIBLE);
    	}
    	roomNameTextField.setEnabled(enabled);
    	createButton.setEnabled(enabled);
    }
    
	public void onClickCreateRoom(View v){
        // Room input
         
         roomName = roomNameTextField.getText().toString();
         Toast.makeText(this, "ROOM: " + roomName, Toast.LENGTH_SHORT).show();
        
        if(roomName.length() == 0) {
        	Toast.makeText(this, "You need to enter a room name", Toast.LENGTH_SHORT).show();
        } else {
		    // Try to join the room
		    new HTTPAsyncTask(this).execute(getString(R.string.baseURL) + "/createRoom.php?roomID=" + roomName + "&&androidID=" + deviceUuid);
		    setInputEnabled(false);
        }
		 
		 
    }
  

	public void httpTaskCallback(String results) {
        //Do anything with response..
		setInputEnabled(true);
		
		
		/*TextView errorText = (TextView) findViewById(R.id.textdisplay01);
		errorText.setText(results);*/
       
        if(results.equals("Room created")) {
           	Intent intent = new Intent(this, TeacherInRoomActivity.class);
    	    intent.putExtra("roomName", roomName);
    	    startActivity(intent);
    	    Toast.makeText(this, "Room created", Toast.LENGTH_SHORT).show();
        } else if(results.equals("Room already exists")){
        	Toast.makeText(this, "Room already exists", Toast.LENGTH_SHORT).show();
        } else {
        	Toast.makeText(this, "Make sure connection works", Toast.LENGTH_SHORT).show();
        	Log.d("speedappteacher", "Error, can't create room! " + results);
        }
		
	}
 
}
